package com.wxzz.elearning.common.bean;


import com.wxzz.elearning.common.constants.RabbitPool;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.SerializerMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.HashMap;
import java.util.Map;

/**
 * 订阅模式队列
 *
 * @author zhangjunjie
 * @date 2021/6/8
 */
@Configuration
public class RabbitMQConfiguration {

    @Bean
    @Scope("prototype")
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMandatory(true);
        template.setMessageConverter(new SerializerMessageConverter());
        return template;
    }

    @Bean
    public Queue dirQueue() {
        return new Queue(RabbitPool.DEFAULT_QUEUE);
    }


    @Bean
    public Queue systemMessage() {
        return new Queue(RabbitPool.ACK_QUEUE_CALLBACK_SYSTEM_MESSAGE);
    }

    @Bean
    public Queue sms() {
        return new Queue(RabbitPool.ACK_QUEUE_CALLBACK_SMS);
    }

    @Bean
    public Queue subscribe() {
        return new Queue(RabbitPool.ACK_QUEUE_CALLBACK_SUBSCRIBE);
    }

    //注册了一个fanout类型的exchange。 广播模式
    @Bean
    public FanoutExchange ackFanoutExchange() {
        return new FanoutExchange(RabbitPool.ACK_FANOUT_EXCHANGE);
    }

    @Bean
    public Binding ackBindSystemMessage() {
        return BindingBuilder.bind(systemMessage()).to(ackFanoutExchange());
    }

    @Bean
    public Binding ackBindSms() {
        return BindingBuilder.bind(sms()).to(ackFanoutExchange());
    }

    @Bean
    public Binding ackBindSubscribe() {
        return BindingBuilder.bind(subscribe()).to(ackFanoutExchange());
    }



    /*//队列名称
    public final static String livetelecastQueue = "livetelecastQueue";

    //交换机名称
    public final static String livetelecastExchange = "livetelecast_exchange";

    //routingKey
    public final static String routingKeyLivetelecast = "routing_key_livetelecast";

    //死信消息队列名称
    public final static String deadLetterQueueLivetelecast = "deadLetterQueueLivetelecast";

    //死信交换机名称
    public final static String dealExchangeLivetelecast = "dealExchangeLivetelecast";

    //死信 routingKey
    public final static String deadRoutingKeyLivetelecast = "dead_routing_key_livetelecast";*/

    //死信队列 交换机标识符
    public static final String DEAD_LETTER_QUEUE_KEY = "x-dead-letter-exchange";

    //死信队列交换机绑定键标识符
    public static final String DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";

    // 队列类型：直播间状态
    public static final String QUEUE_TYPE1 = "livetelecastStatus";

    // 队列类型：直播推流状态
    public static final String QUEUE_TYPE2 = "pushStatus";

    // 队列类型：直播间预约提醒
    public static final String QUEUE_TYPE3 = "livetelecastMessage";

    // 队列类型：培训计划即将到期提醒
    public static final String QUEUE_TYPE4 = "willStudyPlanRemind";

    // 队列类型：培训计划到期提醒
    public static final String QUEUE_TYPE5 = "studyPlanRemind";

    // 队列类型：活动开始状态修改
    public static final String QUEUE_TYPE6 = "activityStartStatus";

    // 队列类型：活动结束状态修改
    public static final String QUEUE_TYPE7 = "activityEndStatus";

    // 队列类型：通知的短信延迟发送
    public static final String QUEUE_TYPE8 = "smsSend";

    // 队列类型：活动预约提醒
    public static final String QUEUE_TYPE9 = "activityMessage";

    // 队列类型：培训调查状态自动变更
    public static final String QUEUE_TYPE10 = "trainSurvey";
    /**
     * 非阻塞版
     * 参考： https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases
     * 使用 rabbitmq_delayed_message_exchange 插件，通过 x-delayed-message的交换机实现
     */

    public final static String WXZZ_DELAY_EXCHANGE = "wxzz-delay-exchange";
    //直播模块
    public final static String WXZZ_DELAY_QUEUE = "wxzz-live-delay-queue";
    //课程模块
    public final static String WXZZ_COURSE_DELAY_QUEUE = "wxzz-course-delay-queue";
    //短信通知模块
    public final static String WXZZ_MESSAGE_DELAY_QUEUE = "wxzz-message-delay-queue";
    //直播路由key
    public final static String WXZZ_DELAY_KEY = "wxzz.live.key.delay";
    //课程路由key
    public final static String WXZZ_COURSE_DELAY_KEY = "wxzz.course.key.delay";
    //短信通知路由key
    public final static String WXZZ_MESSAGE_DELAY_KEY = "wxzz.message.key.delay";


    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //自定义交换机
        return new CustomExchange(WXZZ_DELAY_EXCHANGE, "x-delayed-message", false, false, args);
    }

    @Bean
    public Queue liveDelayQueue() {
        return new Queue(WXZZ_DELAY_QUEUE, false, false, false);
    }
    @Bean
    public Queue courseDelayQueue() {
        return new Queue(WXZZ_COURSE_DELAY_QUEUE, false, false, false);
    }
    @Bean
    public Queue messageDelayQueue() {
        return new Queue(WXZZ_MESSAGE_DELAY_QUEUE, false, false, false);
    }
    @Bean
    public Binding bindingLiveDelayExchangeAndQueue() {
        return BindingBuilder.bind(liveDelayQueue()).to(delayExchange()).with(WXZZ_DELAY_KEY).noargs();
    }

    @Bean
    public Binding bindingCourseDelayExchangeAndQueue() {
        return BindingBuilder.bind(courseDelayQueue()).to(delayExchange()).with(WXZZ_COURSE_DELAY_KEY).noargs();
    }

    @Bean
    public Binding bindingMessageDelayExchangeAndQueue() {
        return BindingBuilder.bind(messageDelayQueue()).to(delayExchange()).with(WXZZ_MESSAGE_DELAY_KEY).noargs();
    }
   /*
   // 阻塞版
   @Autowired
    private CachingConnectionFactory connectionFactory;

    // 创建死信交换机
    @Bean
    public DirectExchange dealExchangeLivetelecast() {
        return new DirectExchange("dealExchangeLivetelecast");
    }

    // 创建普通队列
    @Bean
    public Queue livetelecastQueue() {
        return new Queue("livetelecastQueue", true, false, false);
    }

    // 绑定Queue队列到交换机,并且指定routingKey
    @Bean
    Binding repeatTradeBinding() {
        return BindingBuilder.bind(livetelecastQueue()).to(dealExchangeLivetelecast()).with("livetelecastQueue");
    }

    //创建配置死信队列
    @Bean
    public Queue deadLetterQueueLivetelecast() {
        Map<String,Object> args = new HashMap<>();
        // args.put("x-message-ttl", 5 * 1000); // 直接设置 Queue 延迟时间 但如果直接给队列设置过期时间,这种做法不是很灵活
        args.put( DEAD_LETTER_QUEUE_KEY , "dealExchangeLivetelecast");
        args.put( DEAD_LETTER_ROUTING_KEY, "livetelecastQueue");
        Queue queue = new Queue("deadLetterQueueLivetelecast", true, false, false, args);
        return queue;
    }
*/
}
